/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.Util;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.History;
import com.smartgwt.client.util.SC;

public class UploadItemCallback
{

	public static void formCallback(String msg)
	{
		JSONValue value = JSONParser.parse(msg);
		JSONObject obj = value.isObject();
		if (!obj.get("status").isBoolean().booleanValue())
		{
			SC.warn(obj.get("msg").isString().stringValue(), StaticHolder.dumpB);
		} else
		{
			String token = obj.get("token").isString().stringValue();
			//SC.say(token);
			if (token.startsWith("user_jobs"))
			{
				if (StaticHolder.jobGrid!=null)
					StaticHolder.jobGrid.reload();
			} else if (token.startsWith("user_templates"))
			{
				if (StaticHolder.templateGrid!=null)
				{
					StaticHolder.templateGrid.reload();
				}
			}
			History.newItem(token);
		}
	}

	public static native void setCallback() /*-{
	    $wnd._jasonUploadCallback =
	       @net.bioteam.appweb.gwt.apps.client.Util.UploadItemCallback::formCallback(Ljava/lang/String;);
	 }-*/;

	/*
	 * the following method should be called if the callback method is not
	 * static.
	 */
	// public native void setCallback()
	// /*-{
	// var obj=this;
	// $wnd._jasonUploadCallback=
	// function(x) {
	// obj.@person.jason.client.TestForm::myalert(Ljava/lang/String;)(x);
	// };
	//
	// }-*/;
	//	
}
